using System.Xml;

namespace NGIS.Model
{
	public class ModelClass : IModelClass
	{
		private string mName;

		private string mUID;

		private EExecutionStyle mExecutionStyle;

		private ModelAttribute mModelAttribute;

		private ModelBehavior mModelBehavior;

		private ModelRuntime mModelRuntime;

		public ModelClass()
		{
			mName = "Model Name";
			mUID = "";
			mExecutionStyle = EExecutionStyle.EES_SIMPLE_CALCULATION;
			mModelAttribute = new ModelAttribute();
			mModelBehavior = new ModelBehavior();
			mModelRuntime = new ModelRuntime();
		}

		public void setName(string pName)
		{
			mName = pName;
		}

		public string getName()
		{
			return mName;
		}

		public void setUID(string pUID)
		{
			mUID = pUID;
		}

		public string getUID()
		{
			return mUID;
		}

		public void setExecutionStyle(EExecutionStyle pStyle)
		{
			mExecutionStyle = pStyle;
		}

		public EExecutionStyle getExecutionStyle()
		{
			return mExecutionStyle;
		}

		public ModelAttribute getModelAttribute()
		{
			return mModelAttribute;
		}

		public ModelBehavior getModelBehavior()
		{
			return mModelBehavior;
		}

		public ModelRuntime getModelRuntime()
		{
			return mModelRuntime;
		}

		public bool LoadFromXmlFile(string fileName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Prefix = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
			xmlDocument.Load(fileName);
			XmlElement documentElement = xmlDocument.DocumentElement;
			mName = documentElement.GetAttribute("name");
			mUID = documentElement.GetAttribute("uid");
			string a = "";
			if (documentElement.HasAttribute("style"))
			{
				a = documentElement.GetAttribute("style");
			}
			else if (documentElement.HasAttribute("type"))
			{
				a = documentElement.GetAttribute("type");
			}
			if (a == "SimpleCalculation")
			{
				mExecutionStyle = EExecutionStyle.EES_SIMPLE_CALCULATION;
			}
			else if (a == "TimeSeries")
			{
				mExecutionStyle = EExecutionStyle.EES_TIME_SERIES;
			}
			else
			{
				mExecutionStyle = EExecutionStyle.EES_STATE_SIMULATION;
			}
			mModelAttribute.loadFromXml(documentElement);
			mModelBehavior.loadFromXml(documentElement);
			mModelRuntime.loadFromXml(documentElement);
			return true;
		}

		public bool FormatToXmlFile(string fileName)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlElement xmlElement = xmlDocument.CreateElement("ModelClass");
			xmlElement.SetAttribute("name", mName);
			xmlElement.SetAttribute("uid", mUID);
			if (mExecutionStyle == EExecutionStyle.EES_SIMPLE_CALCULATION)
			{
				xmlElement.SetAttribute("style", "SimpleCalculation");
			}
			else if (mExecutionStyle == EExecutionStyle.EES_TIME_SERIES)
			{
				xmlElement.SetAttribute("style", "TimeSeries");
			}
			else
			{
				xmlElement.SetAttribute("style", "StateSimulation");
			}
			xmlDocument.AppendChild(xmlElement);
			mModelAttribute.formatToXml(xmlElement);
			mModelBehavior.formatToXml(xmlElement);
			mModelRuntime.formatToXml(xmlElement);
			xmlDocument.Save(fileName);
			return true;
		}

		public bool LoadFromXmlStream(string xmlStr)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.LoadXml(xmlStr);
			XmlElement documentElement = xmlDocument.DocumentElement;
			mName = documentElement.GetAttribute("name");
			mUID = documentElement.GetAttribute("uid");
			string attribute = documentElement.GetAttribute("style");
			if (attribute == "SimpleCalculation")
			{
				mExecutionStyle = EExecutionStyle.EES_SIMPLE_CALCULATION;
			}
			else if (attribute == "TimeSeries")
			{
				mExecutionStyle = EExecutionStyle.EES_TIME_SERIES;
			}
			else
			{
				mExecutionStyle = EExecutionStyle.EES_STATE_SIMULATION;
			}
			mModelAttribute.loadFromXml(documentElement);
			mModelBehavior.loadFromXml(documentElement);
			mModelRuntime.loadFromXml(documentElement);
			return true;
		}

		public bool FormatToXmlStream(ref string xmlStr)
		{
			XmlDocument xmlDocument = new XmlDocument();
			XmlElement xmlElement = xmlDocument.CreateElement("ModelClass");
			xmlElement.SetAttribute("name", mName);
			xmlElement.SetAttribute("uid", mUID);
			if (mExecutionStyle == EExecutionStyle.EES_SIMPLE_CALCULATION)
			{
				xmlElement.SetAttribute("style", "SimpleCalculation");
			}
			else if (mExecutionStyle == EExecutionStyle.EES_TIME_SERIES)
			{
				xmlElement.SetAttribute("style", "TimeSeries");
			}
			else
			{
				xmlElement.SetAttribute("style", "StateSimulation");
			}
			xmlDocument.AppendChild(xmlElement);
			mModelAttribute.formatToXml(xmlElement);
			mModelBehavior.formatToXml(xmlElement);
			mModelRuntime.formatToXml(xmlElement);
			xmlStr = xmlDocument.InnerXml;
			return true;
		}

		public bool compareOther(IModelClass pClass, bool withRuntime, ref string obj, ref string name)
		{
			bool flag = false;
			if (mModelAttribute != null && !mModelAttribute.compareOther(pClass.getModelAttribute(), ref obj, ref name))
			{
				return false;
			}
			if (mModelBehavior != null && !mModelBehavior.compareOther(pClass.getModelBehavior(), ref obj, ref name))
			{
				return false;
			}
			if (withRuntime && mModelRuntime != null && !mModelRuntime.compareOther(pClass.getModelRuntime(), ref obj, ref name))
			{
				return false;
			}
			return true;
		}
	}
}
